package com.gusha.app;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.dao.SysAssetsDao;
import com.gusha.dao.SysGroundDao;
import com.gusha.dao.SysOrderDao;
import com.gusha.dao.SysTicketDao;
import com.gusha.dao.UserDao;
import com.gusha.dao.UserMoneyDao;
import com.gusha.model.SysAssets;
import com.gusha.model.SysGround;
import com.gusha.model.SysOrder;
import com.gusha.model.SysTicket;
import com.gusha.model.User;
import com.gusha.model.UserMoney;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@CrossOrigin(origins="*",maxAge=3600)
@Controller
@RequestMapping("/app/MyMoney")
public class AppMyMoney {
		@Autowired
		private UserMoneyDao userMoneyDao;
		@Autowired
		private SysOrderDao sysOrderDao;
		@Autowired
		private SysGroundDao sysGroundDao;
		@Autowired
		private SysAssetsDao sysAssetsDao;
		@Autowired
		private SysTicketDao STDao;
		@Autowired
		private UserDao userDao;
		
		
		//设置余额支付密码
		@CrossOrigin(origins="*",maxAge=3600)
		@RequestMapping("/toAddPassword")
		@ResponseBody
		public JsonResult toAddPassword(Model model,HttpServletRequest request) {
			JsonResult result=new JsonResult(false);
			String user = WebValueUtil.getWebValue(request, "user");
			String password = WebValueUtil.getWebValue(request, "password");
			if(StringUtils.isNotBlank(user)&&StringUtils.isNotBlank(password)) {
				User uid=new User();
				uid.setId(new BigInteger(user));
			UserMoney userMoney  =	userMoneyDao.findAllByUser(uid);
			userMoney.setPassword(password);
			userMoneyDao.saveAndFlush(userMoney);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
			}
			return result;
			
		}
		
		//进入修改密码
		@CrossOrigin(origins="*",maxAge=3600)
		@RequestMapping("/toUpdatePWD")
		@ResponseBody
		public 	JsonResult toUpdatePWD(Model model,HttpServletRequest request) {
			JsonResult result=new JsonResult(false);
			String user = WebValueUtil.getWebValue(request, "user");
			if(StringUtils.isNotBlank(user)) {
				User uid=new User();
				uid.setId(new BigInteger(user));
				UserMoney userMoney=userMoneyDao.findAllByUser(uid);
				if(userMoney.getPassword()!=null) {
					String Oldpwd=userMoney.getPassword();
					result.setMsg("操作成功");
					result.setObj(Oldpwd);
					result.setFlag(true);
				}
				result.setMsg(null);
				result.setMsg("未设置密码");
				
			}
			return result;
		}
		
		//余额支付商城订单
		@CrossOrigin(origins="*",maxAge=3600)
		@RequestMapping("/toPayShopOrder")
		@ResponseBody
		public JsonResult toPayShopOrder(Model model,HttpServletRequest request) {
			JsonResult result=new JsonResult(false);
			String user = WebValueUtil.getWebValue(request, "user");
			String password = WebValueUtil.getWebValue(request, "password");
			String money = WebValueUtil.getWebValue(request, "money");
			
			if(StringUtils.isNotBlank(money)&&StringUtils.isNotBlank(user)&&StringUtils.isNotBlank(password)) {
				User uid=new User();
				uid.setId(new BigInteger(user));
				UserMoney um=userMoneyDao.findAllByUser(uid);
				double myM=um.getMoney().doubleValue();
				double mo=new BigDecimal(money).doubleValue();
				
				
				if(myM>mo) {//判断数据库余额大于支付金额
					if(um.getPassword().equals(password)) {
					BigDecimal	last=um.getMoney().subtract(new BigDecimal(money));
						um.setMoney(last);
						userMoneyDao.saveAndFlush(um);//减去数据库余额
						List<SysOrder> list=sysOrderDao.findAllByUserIdAndState(uid, new BigInteger("1"));
						for (SysOrder sysOrder : list) {
							sysOrder.setState(new BigInteger("2"));
							sysOrderDao.saveAndFlush(sysOrder);
							
							//如果这个产品名存在
							SysAssets assets=sysAssetsDao.findAllByUserIdAndShoppingName(uid,sysOrder.getShoppingId().getCommodityName());
							if(assets!=null) {
								assets.setShoppingNum(assets.getShoppingNum().longValue()+sysOrder.getCommodityNum().longValue()); 
								sysAssetsDao.saveAndFlush(assets);
							}else {//如果这个产品名不存在
						
							// 添加资产
							SysAssets sysAssets = new SysAssets();
							sysAssets.setUserId(uid);
							sysAssets.setShoppingType(sysOrder.getShoppingId().getCommodityType()); // 添加商品类型
							sysAssets.setShoppingName(sysOrder.getShoppingId().getCommodityName()); // 添加商品名字
							sysAssets.setShoppingImg(sysOrder.getShoppingId().getCommodityImg()); // 添加商品图片
							sysAssets.setShoppingNum(sysOrder.getCommodityNum().longValue()); // 添加购买的商品数量
							sysAssets.setPayTime(sysOrder.getAddTime()); // 添加购买商品时间
							sysAssets.setStealNum(0L); // 偷来的数量为 0
							sysAssets.setPlantNum(0L); // 偷来的数量为 0
							sysAssets.setFinishedNum(0L); // 偷来的数量为 0
							sysAssetsDao.saveAndFlush(sysAssets);
							
							}
							
						}
						
						
						result.setMsg("支付成功");
						result.setFlag(true);
						return result;
					}
					
					result.setMsg("密码输入有误");
					return result;
				}
				result.setMsg("余额不足");
				return result;
			}
		return result;
		
	}
		
		//余额支付土地订单
		@CrossOrigin(origins="*",maxAge=3600)
		@RequestMapping("/toPayGroudOrder")
		@ResponseBody
		public JsonResult toPayGroudOrder(Model model,HttpServletRequest request) {
			JsonResult result=new JsonResult(false);
			String user = WebValueUtil.getWebValue(request, "user");
			String password = WebValueUtil.getWebValue(request, "password");
			String money = WebValueUtil.getWebValue(request, "money");
			//String groudId = WebValueUtil.getWebValue(request, "groudId");

			if(StringUtils.isNotBlank(money)&&StringUtils.isNotBlank(user)) {
				User uid=new User();
				uid.setId(new BigInteger(user));
				UserMoney um=userMoneyDao.findAllByUser(uid);
				double myM=um.getMoney().doubleValue();
				double mo=new BigDecimal(money).doubleValue();
				if(myM>mo) {//判断数据库余额大于支付金额
					if(um.getPassword().equals(password)) {
					BigDecimal	last=um.getMoney().subtract(new BigDecimal(money));
						um.setMoney(last);
						userMoneyDao.saveAndFlush(um);//减去数据库余额
						List<SysGround> list=sysGroundDao.findAllByUserId(uid);
						for (SysGround sysGround : list) {
							sysGround.setPaymentState(new BigInteger("1"));
							sysGroundDao.saveAndFlush(sysGround);
							
							
						}
						result.setMsg("支付成功");
						result.setFlag(true);
						return result;
						}
						
					result.setMsg("密码输入有误");
					return result;
					}
				result.setMsg("余额不足");
				return result;
					
				}
			return result;
			}
		
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toPayMyTicket")
	@ResponseBody
	public JsonResult toPayMyTicket(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String user = WebValueUtil.getWebValue(request, "user");
		String password = WebValueUtil.getWebValue(request, "password");
		String money = WebValueUtil.getWebValue(request, "money");
		if(StringUtils.isNotBlank(money)&&StringUtils.isNotBlank(user)&&StringUtils.isNotBlank(password)) {
			User uid=new User();
			uid.setId(new BigInteger(user));
			UserMoney um=userMoneyDao.findAllByUser(uid);
			double myM=um.getMoney().doubleValue();
			double mo=new BigDecimal(money).doubleValue();
			if(myM>mo) {//判断数据库余额大于支付金额
				if(um.getPassword().equals(password)) {
				BigDecimal	last=um.getMoney().subtract(new BigDecimal(money));
					um.setMoney(last);
					userMoneyDao.saveAndFlush(um);//减去数据库余额
					List<SysTicket> list=STDao.findAllByUserId(uid);
					for (SysTicket st : list) {
						st.setTicketState(new BigInteger("1"));
						STDao.saveAndFlush(st);
						
						
					}
					result.setMsg("支付成功");
					result.setFlag(true);
					return result;
					}
					
				result.setMsg("密码输入有误");
				return result;
				}
			result.setMsg("余额不足");
			return result;
				
			}
		return result;}	
	
	//用户领取体验金
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toTestMoney")
	@ResponseBody
	public JsonResult toTestMoney(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String user = WebValueUtil.getWebValue(request, "user");
		String money = WebValueUtil.getWebValue(request, "money");
		if(StringUtils.isNotBlank(user)&&StringUtils.isNotBlank(money)) {
			User uid=userDao.getOne(new BigInteger(user));
			
			if(uid.getTestMoney().toString().equals("0")) {
		
			UserMoney um=new UserMoney();
			UserMoney um1=userMoneyDao.findAllByUser(uid);
			if(um1==null) {
			um.setUser(uid);
			double	YE=new BigDecimal(money).doubleValue();
			um.setMoney(new BigDecimal(YE));
			userMoneyDao.saveAndFlush(um);
			}
			if(um1!=null) {
				if(um1.getMoney().toString()==null||um1.getMoney().toString().equals("")) {
			
		double	YE=new BigDecimal(money).doubleValue();
				um1.setMoney(new BigDecimal(YE));
				}else {
					double	YE=um1.getMoney().doubleValue()+new BigDecimal(money).doubleValue();
					um1.setMoney(new BigDecimal(YE));
				}
				
			
			userMoneyDao.saveAndFlush(um1);
			}
			uid.setTestMoney(new BigInteger("1"));
			userDao.saveAndFlush(uid);
			result.setMsg("领取成功");
			result.setFlag(true);
			return result;
			}
			result.setMsg("已经领取过了");
			
			
			
		}
		return result;
	}
		
}
